﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class Dialog
    {
        private string Text;
        private List<string> TextList;

        public Dialog(int DialogID)        
        {
            this.Text = LanguageManager.GetString(DialogID);

            if (this.Text.Contains("(BREAK)"))
            {
                string[] str = new string[]
                {
                    "(BREAK)"
                };
                TextList = Text.Split(str, StringSplitOptions.RemoveEmptyEntries).ToList();

                List<string> toRemove = new List<string>();
                for (int i = 0; i < TextList.Count; i++)
                {
                    if (string.IsNullOrEmpty(TextList[i]))
                    {
                        toRemove.Add(TextList[i]);
                    }
                }
                for (int i = 0; i < toRemove.Count; i++)
                {
                    TextList.Remove(toRemove[i]);
                }
            }
            else
            {
                TextList = new List<string>();
                TextList.Add(Text);
            }
        }

        public bool PlayerQuicked;
        public bool Finished;

        public event EventHandler FinishedEvent;

        public virtual void Update()
        {
            if (Engine.Input.IsActivePlayerButtonPress(Microsoft.Xna.Framework.Input.Buttons.A) ||
                Engine.Input.IsKeyboardPress(Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                if (PlayerQuicked)
                {
                    Finished = true;
                    if (FinishedEvent != null)
                        FinishedEvent(this, EventArgs.Empty);
                }
                else
                {
                    PlayerQuicked = true;
                }
            }
        }

        private int charactersDone;
        private int collumDone;
        public float TimeToNextCharacter = 32;
        private float timer;

        private Vector2 ButtonScale;

        public virtual void Draw()
        {
            if (!PlayerQuicked)
            {
                timer += Engine.GameTime.ElapsedGameTime.Milliseconds;
                if (timer > TimeToNextCharacter)
                {
                    charactersDone++;
                    if (charactersDone >= TextList[collumDone].Length &&
                        TextList.Count > collumDone + 1)
                    {
                        charactersDone = 0;
                        collumDone++;
                    }
                    if (charactersDone >= TextList[collumDone].Length &&
                        TextList.Count <= collumDone + 1)
                    {
                        PlayerQuicked = true;
                    }
                    timer = 0;
                }
                for (int i = 0; i <= collumDone; i++)
                {
                    string finalString = null;
                    if (i == collumDone)
                    {
                        if (charactersDone < TextList[i].Length)
                        {
                            finalString = TextList[i].Remove(charactersDone, (TextList[i].Length - 1) - charactersDone);
                        }
                        else
                        {
                            finalString = TextList[i];
                        }
                    }
                    else
                    {
                        finalString = TextList[i];
                    }
                    Vector2 finalPos = new Vector2(Engine.RenderManager.CenterTextX(TextList[i], "Kootenay", 1), (i * 50) + 460);
                    Engine.RenderManager.DrawString(finalString, 2, 255, finalPos, "Kootenay", Color.White, null);
                }
            }
            else
            {
                if (ButtonScale.X < 1)
                {
                    ButtonScale += Engine.SpeedFactor * new Vector2(0.1f);
                }
                for (int i = 0; i < TextList.Count; i++)
                {
                    Vector2 finalPos = new Vector2(Engine.RenderManager.CenterTextX(TextList[i], "Kootenay", 1), (i * 50) + 460);
                    Engine.RenderManager.DrawString(TextList[i], 2, 255, finalPos, "Kootenay", Color.White, null);
                }
            }

            Engine.RenderManager.DrawStringWithButton(LanguageManager.GetEngineString(4), 2, 255,
                new Vector2(900, 650), "Kootenay", Color.White, 0, Vector2.Zero, ButtonScale, 0, ButtonScale,
                null);
        }
    }
}
